var util = require('../../utils/util.js');
var areaTool = require('../../utils/area.js');

var app = getApp();
Page({
  data: {
    shop_name: '', //门店名称
    true_name: '',
    mob_phone: '',
    house_number: '',
    province_id: '', //省id
    city_id: '', //市id
    area_id: '', //区id
    addressDetail: '', //详细地址
    hasAddress: true,
    province: '', //省
    city: '', //市
    area: '', //区
    provinces: [],
    citys: [],
    areas: [],
    AreaJson: [],
    show: false,
    lat_x: '', //经度
    lat_y: '', //纬度
    is_default: 0,
    flag: 0, //0门店注册,1门店编辑
    item: {}, //编辑的回填数据
  },
  onLoad: function(option) {
    var that = this;
    console.log("onload", option);
    if (option.item) {
      that.data.flag = 1;
      wx.setNavigationBarTitle({
        title: '门店编辑',
      })
      var item = JSON.parse(option.item);
      console.log("onload", item);
      var mylocation = item.area_info.split(' ');
      that.setData({
        shop_name: item.shop_name,
        true_name: item.true_name,
        mob_phone: item.mob_phone,
        province: mylocation[0],
        city: mylocation[1],
        area: mylocation[2],
        addressDetail: item.address,
        house_number: item.house_number,
        is_default: item.is_default,
        item: item,
      })
      if (item.is_default == 1) {
        that.setData({
          switch_chekced: true,
        })
      } else {
        that.setData({
          switch_chekced: false,
        })
      }
      app.province_id = item.province_id;
      app.city_id = item.city_id;
      app.area_id = item.area_id;
    }

    util.postUrl('area/get_list', {}, function(res) {
      if (res.data.code == 200) {
        var AreaJson = res.data.datas;
        console.log("AreaJson = ", AreaJson);
        areaTool.setAreaJson(AreaJson);
        var provinces = [];
        for (var i = 0; i < AreaJson.length; i++) {
          provinces.push(AreaJson[i]);
        }
        if (that.data.flag == 0) {
          app.province_id = provinces[0].area_id;
          app.city_id = areaTool.getCitys(0)[0].area_id;
          app.area_id = areaTool.getAreas(0, 0)[0].area_id;
        }
        that.setData({
          provinces: provinces,
          citys: areaTool.getCitys(0),
          areas: areaTool.getAreas(0, 0),
        })
      }
    })
  },
  // 选择省市区
  sureSelectAreaListener: function(e) {
    console.log('3333', e.detail.currentTarget.dataset)
    var that = this;
    that.setData({
      show: false,
      province: e.detail.currentTarget.dataset.province,
      city: e.detail.currentTarget.dataset.city,
      area: e.detail.currentTarget.dataset.area
    })
  },
  chooseAddress: function() {
    console.log("选择地址")
    var that = this;
    that.setData({
      show: true
    })
  },
  // 手机号码
  shopMobile: function(e) {
    let that = this;
    let myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    this.setData({
      mob_phone: e.detail.value
    });
  },
  getAddress(e) {
    var that = this;
    wx.chooseLocation({
      success(res) {
        console.log('22222222222', res.latitude, res.longitude)
        that.setData({
          addressDetail: res.name,
          hasAddress: false,
          lat_y: res.latitude, //纬度
          lat_x: res.longitude //经度
        })
      }
    });
  },
  // 表单提交：
  formSubmit: function(e) {
    console.log(e.detail.value);
    var that = this;
    let shopName = e.detail.value.shopName;
    let bossName = e.detail.value.bossName;
    let bossPhone = e.detail.value.bossPhone;
    let address = e.detail.value.address;
    let myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (shopName == '') {
      wx.showToast({
        title: '门店名称为空!',
        icon: 'none'
      })
      return false;
    }
    if (bossName == '') {
      wx.showToast({
        title: '负责人为空！',
        icon: 'none'
      })
      return false;
    }
    if (bossPhone == '') {
      wx.showToast({
        title: '手机号码为空！',
        icon: 'none'
      })
      return false;
    }
    if (!myreg.test(bossPhone)) {
      wx.showToast({
        title: '请填写正确手机号码！',
        icon: 'none'
      })
      return false;
    }
    if (this.data.province == '') {
      wx.showToast({
        title: '请选择所属区域',
        icon: 'none'
      })
      return false;
    }
    if (address == '') {
      wx.showToast({
        title: '请选择所属区域',
        icon: 'none'
      })
      return false;
    } else {
      //门店编辑
      if (that.data.flag == 1) {
        util.postUrl('member_address/edit_address', {
          address_id: that.data.item.address_id, //编辑的地址id
          shop_name: shopName, //门店名称
          true_name: bossName, //负责人名称
          mob_phone: bossPhone, //联系电话
          // lat_x: this.data.lat_x, //经度
          // lat_y: this.data.lat_y, //纬度
          address: address, //详细地址
          province_id: app.province_id, //省id
          city_id: app.city_id, //市id
          area_id: app.area_id, //区id
          is_default: that.data.is_default, //是否为默认
        }, function(res) {
          if (res.data.code == 200) {
            wx.navigateBack({

            })
          }
        })
        return;
      }

      // 门店注册
      util.postUrl('member_address/add_address', {
        shop_name: shopName, //门店名称
        true_name: bossName, //负责人名称
        mob_phone: bossPhone, //联系电话
        // lat_x: this.data.lat_x, //经度
        // lat_y: this.data.lat_y, //纬度
        address: address, //详细地址
        province_id: app.province_id, //省id
        city_id: app.city_id, //市id
        area_id: app.area_id, //区id
        is_default: that.data.is_default,
      }, function(res) {
        if (res.data.code == 200) {
          wx.navigateBack({

          })
        }
      })
    }
  },
  switchChange: function(e) {
    console.log(e)
    var that = this;
    var value = e.detail.value;
    if (value) {
      that.setData({
        is_default: 1,
      })
    } else {
      that.setData({
        is_default: 0,
      })
    }
  }

})